{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字典\n",
    "字典(dictionary)是另一种`可变`容器模型，且可存储任意类型对象，用dict()表示。  \n",
    "字典的每个键值{key:value}对用冒号`:`分割，每个对之间用逗号`,`分割，整个字典包括在花括号`{}`中\n",
    "\n",
    "> 键必须是`唯一`的，但值不必。值可以取任何数据类型，但键必须是不可变的，如字符串，数字或元组。\n",
    "\n",
    "### 基本操作\n",
    "字典对象是以键值对的形式存储数据的，所以只要知道键，就能得到值。这本质上就是一种映射关系。  \n",
    "既然字典是键值对的映射，就不用考虑所谓“排序”问题了，只要通过键就能找到值，至于这个键值对位置在哪里就不用考虑了。没有排序也就不用索引和切片了\n",
    "> 字典中的这类以键值对的映射方式存储数据，是一种非常高效的方法，比如要读取值得时候，  \n",
    "> 如果用列表，Python需要从头开始读，直到找到指定的那个索引值。但是，在字典中是通过“键”来得到值。要高效得多。  \n",
    "> 正是这个特点，键值对这样的形式可以用来存储大规模的数据，因为检索快捷。规模越大越明显。  \n",
    "> 所以，mongdb这种非关系型数据库在大数据方面比较流行了。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### len(d)\n",
    "返回字典(d)中的键值对的数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {1: 'a', 'b': 2}\n",
    "len(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### d[key]\n",
    "返回字典(d)中的键(key)的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {1: 'a', 'b': 2}\n",
    "d['b']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### d[key] = value\n",
    "更新已有key的value或添加一对键值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: 'c', 'b': 2}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {1:'a','b':2}\n",
    "d[1] = 'c'\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: 'c', 'b': 2, 3: 'python'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d[3] = 'python'\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### del d[key]\n",
    "删除字典(d)的键(key)项（将该键值对删除）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: 'a'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {1:'a','b':2}\n",
    "del d['b']\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### key in d\n",
    "检查字典(d)中是否含有键为key的项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {1:'a','b':2}\n",
    "'b' in d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'c' in d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 常用字典方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['clear',\n",
       " 'copy',\n",
       " 'fromkeys',\n",
       " 'get',\n",
       " 'items',\n",
       " 'keys',\n",
       " 'pop',\n",
       " 'popitem',\n",
       " 'setdefault',\n",
       " 'update',\n",
       " 'values']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[i for i in dir(dict()) if not i.startswith('_')]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### dict.copy()\n",
    "函数返回一个字典的浅复制"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### dict.clear()\n",
    "将字典`清空`(del是删除)，修改原对象，`没有`返回值\n",
    "\n",
    "#### dict.get(key, default=None)\n",
    "返回指定键的值，如果值不在字典则`返回`默认值\n",
    "\n",
    "#### dict.setdefault(key, default=None)\n",
    "`返回`指定键的值，如果值不在字典中则添加这对(key:default)`没有`返回值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### items keys values\n",
    "\n",
    "```\n",
    "dict.items()    # 以列表的`形式`返回可遍历的(键, 值) 元组数组\n",
    "dict.keys()     # 以列表的`形式`返回字典所有的键\n",
    "dict.values()   # 以列表的`形式`返回字典所有的值\n",
    "```\n",
    "返回的并不是list, 而是[view对象](https://docs.python.org/3/library/stdtypes.html#dict-views)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### dict.pop(key,[,d])\n",
    "删除指定键的键值对，如果key不在字典中则返回d，如果key不在字典中又没有d，则报错"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### dict.update(dict2)\n",
    "更新原字典，`没有`返回值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 1, 'b': 2}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = {'a':1}\n",
    "y = {'b':2}\n",
    "x.update(y)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 1, 'b': 2, 'c': 3}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = [('c',3)]  # 类似dict.items\n",
    "x.update(z)\n",
    "x"
   ]
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4rc1"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": "15",
    "lenType": "10",
    "lenVar": "60"
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
